#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/20 09:25
# @Author  : youfeng
import json

import demjson
import redis

from .data_etl import DataETL
from .redis_key_wrap import RedisKeyWrap


class RedisResultHandler(object):
    __EXPIRE_TIME = 24 * 3600 * 7

    def __init__(self, host, port, password, db, max_connections, log):
        self.log = log

        self.__redis_client = redis.StrictRedis(host=host,
                                                port=port,
                                                password=password,
                                                db=db,
                                                max_connections=max_connections)
        self.__data_etl = DataETL(log)

    def __save(self, key, result_dict):
        json_data_str = json.dumps(result_dict)
        json_dict = json.loads(json_data_str)
        json_str = demjson.encode(json_dict)
        self.__redis_client.setex(key, self.__EXPIRE_TIME, json_str)

    # 存储结果数据到redis
    def save_gsxt(self, company, data_list):
        key = RedisKeyWrap.get_gsxt_result_key(company)
        try:
            result_dict = self.__data_etl.get_gsxt_data(data_list)

            self.__save(key, result_dict)
            return True
        except Exception as e:
            self.log.error("存储redis异常: company = {}".format(company))
            self.log.exception(e)

        return False

    def save_wenshu(self, company, data_list):
        key = RedisKeyWrap.get_wenshu_result_key(company)
        try:
            result_dict = self.__data_etl.get_wenshu_data(data_list)

            self.__save(key, result_dict)
            return True
        except Exception as e:
            self.log.error("存储redis异常: company = {}".format(company))
            self.log.exception(e)

        return False

    def save_news(self, company, data_list):
        key = RedisKeyWrap.get_news_result_key(company)
        try:
            result_dict = self.__data_etl.get_news_data(data_list)

            self.__save(key, result_dict)
            return True
        except Exception as e:
            self.log.error("存储redis异常: company = {}".format(company))
            self.log.exception(e)

        return False

    def save_zhixing(self, company, data_list):
        key = RedisKeyWrap.get_zhixing_result_key(company)
        try:
            result_dict = self.__data_etl.get_zhixing_data(data_list)

            self.__save(key, result_dict)
            return True
        except Exception as e:
            self.log.error("存储redis异常: company = {}".format(company))
            self.log.exception(e)

        return False

    def save_shixin(self, company, data_list):
        key = RedisKeyWrap.get_shixin_result_key(company)
        try:
            result_dict = self.__data_etl.get_shixin_data(data_list)

            self.__save(key, result_dict)
            return True
        except Exception as e:
            self.log.error("存储redis异常: company = {}".format(company))
            self.log.exception(e)

        return False
